package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WebRecycleType;
import com.ruoyi.system.service.IWebRecycleTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 回收分类类型Controller
 * 
 * @author ruoyi
 * @date 2021-05-22
 */
@Controller
@RequestMapping("/system/recycleType")
public class WebRecycleTypeController extends BaseController
{
    private String prefix = "system/recycleType";

    @Autowired
    private IWebRecycleTypeService webRecycleTypeService;

    @RequiresPermissions("system:recycleType:view")
    @GetMapping()
    public String recycleType(@RequestParam(defaultValue = "1") Integer type)
    {
        if(type ==1){
            return prefix + "/recycleType";
        }
        return "system/processType/processType";
    }

    /**
     * 查询回收分类类型树列表
     */
    @RequiresPermissions("system:recycleType:list")
    @PostMapping("/list")
    @ResponseBody
    public List<WebRecycleType> list(WebRecycleType webRecycleType)
    {
        List<WebRecycleType> list = webRecycleTypeService.selectWebRecycleTypeList(webRecycleType);
        return list;
    }

    /**
     * 导出回收分类类型列表
     */
    @RequiresPermissions("system:recycleType:export")
    @Log(title = "回收分类类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WebRecycleType webRecycleType)
    {
        List<WebRecycleType> list = webRecycleTypeService.selectWebRecycleTypeList(webRecycleType);
        ExcelUtil<WebRecycleType> util = new ExcelUtil<WebRecycleType>(WebRecycleType.class);
        return util.exportExcel(list, "回收分类类型数据");
    }

    /**
     * 新增回收分类类型
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id,@RequestParam(defaultValue = "1") Integer type, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("webRecycleType", webRecycleTypeService.selectWebRecycleTypeById(id));
        }
        if(type ==1){
            return prefix + "/add";
        }
        return "system/processType/add";
    }

    /**
     * 新增保存回收分类类型
     */
    @RequiresPermissions("system:recycleType:add")
    @Log(title = "回收分类类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WebRecycleType webRecycleType)
    {
        return toAjax(webRecycleTypeService.insertWebRecycleType(webRecycleType));
    }

    /**
     * 修改回收分类类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id,@RequestParam(defaultValue = "1") Integer type, ModelMap mmap)
    {
        WebRecycleType webRecycleType = null;
        if(id == null || id ==  -1){
            webRecycleType = new WebRecycleType();
        }else{
            webRecycleType = webRecycleTypeService.selectWebRecycleTypeById(id);
        }
        mmap.put("webRecycleType", webRecycleType);
        if(type ==1){
            return prefix + "/edit";
        }
        return "system/processType/edit";
    }

    /**
     * 修改保存回收分类类型
     */
    @RequiresPermissions("system:recycleType:edit")
    @Log(title = "回收分类类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WebRecycleType webRecycleType)
    {
        CacheUtils.removeAll(Constants.RECYCLE_TYPE_KEY);
        return toAjax(webRecycleTypeService.updateWebRecycleType(webRecycleType));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:recycleType:remove")
    @Log(title = "回收分类类型", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        CacheUtils.removeAll(Constants.RECYCLE_TYPE_KEY);
        return toAjax(webRecycleTypeService.deleteWebRecycleTypeById(id));
    }

    /**
     * 选择回收分类类型树
     */
    @GetMapping(value = { "/selectRecycleTypeTree/{id}", "/selectRecycleTypeTree/" })
    public String selectRecycleTypeTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("webRecycleType", webRecycleTypeService.selectWebRecycleTypeById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载回收分类类型树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(Integer type)
    {
        List<Ztree> ztrees = webRecycleTypeService.selectWebRecycleTypeTree(type);
        return ztrees;
    }


    /**
     * ajax读取服务流程
     * @param recycleTypeId
     * @return
     */
    @ResponseBody
    @GetMapping("/getJsonServerFlow/{recycleTypeId}")
    public AjaxResult getJsonServerFlow(@PathVariable(value = "recycleTypeId") Integer recycleTypeId){
        List<WebRecycleType> webRecycleTypeList = webRecycleTypeService.list(new QueryWrapper<WebRecycleType>()
                .eq("parent_id",recycleTypeId)
                .eq("status",0)
                .select("name","id")
        );
        return AjaxResult.success(webRecycleTypeList);
    }

}
